package com.xb.dao;

import com.xb.entity.User;
import com.xb.utils.DataSourceUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDao extends BaseDao {



    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */

    public User findByUsername(String username) {

        User user= null;
        try {
            user = jdbcTemplate.queryForObject("select * from user where username=?",new BeanPropertyRowMapper<>(User.class),username);
        } catch (DataAccessException e) {
          //  throw new RuntimeException(e);
        }
        return user;
    }

    /**
     * 根据email查询用户
     * @param email
     * @return
     */
    public User findByEmail(String email) {
        User user= null;
        try {
            user = jdbcTemplate.queryForObject("select * from user where email=?",new BeanPropertyRowMapper<>(User.class),email);
        } catch (DataAccessException e) {
          //  throw new RuntimeException(e);
        }
        return user;

    }

    //用户注册
    public void register(User user) {
        jdbcTemplate.update("insert into user values (null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                user.getUsername(),
                user.getPassword(),
                user.getEmail(),
                user.getQqOpenid(),
                user.getWxOpenid(),
                user.getRealName(),
                user.getAge(),
                user.getPhone(),
                user.getGender(),
                user.getInfo(),
                user.getRegisterTime(),
                user.getLoginTime(),
                user.getPic(),
                user.getLook(),
                user.getIsSecret(),
                user.getDeptName(),
                user.getDeptId()
        );
    }


    /**
     * 查询用户
     * @return
     */
    public List<User> findAll() {
        return jdbcTemplate.query("select * from user ",new BeanPropertyRowMapper<>(User.class));
    }

    /**
     *搜索用户
     * @return
     */

    public List<User> search(String searchVal) {
        String sql="select * from user where username like ? or real_name like ?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class),"%"+searchVal+"%","%"+searchVal+"%");
    }

    /**
     * 用户搜索+分页
     * @param realName
     * @param startIndex
     * @param pageSize
     * @return
     */
    public List<User> findPage(String realName, Integer startIndex, Integer pageSize) {
        String sql="select * from user where real_name like ? or username like ? limit ?,?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class),"%"+realName+"%", "%"+realName+"%",startIndex,pageSize);

    }

    /**
     * 查询总记录数
     * @param realName
     * @return
     */
    public Integer countByRealName(String realName) {
        String sql="select count(*) from user where real_name like ? or username like ?";
        return jdbcTemplate.queryForObject(sql, Integer.class,"%"+realName+"%" ,"%"+realName+"%");
    }


    /**
     * 根据部门Id去查询用户信息
     * @param deptId
     * @return
     */
    public List<User> findByDeptId(Long deptId) {
        String sql="select * from user where dept_id=?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class),deptId);
    }


    /**
     * 根据id查询用户详情
     * @param userId
     * @return
     */
    public User findById(Long userId) {
        String sql="select * from user where id=?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),userId);
    }


    /**
     * 查询指定用户的粉丝数
     * @param userId
     * @return
     */
    public Integer findFansCount(Long userId) {
        String sql="select count(*) from userfocus uf where user_focus_id=?";
        return jdbcTemplate.queryForObject(sql, Integer.class,userId);
    }


    /**
     * 更新被看数
     * @param userId
     */
    public void updateLook(Long userId) {
        String sql="update user set look=look+1 where id=?";
        jdbcTemplate.update(sql,userId);

    }

    /**
     * 查询当前登录用户的关注数
     * @param id
     * @return
     */
    public Integer findFocusCount(Long id) {
        String sql="select count(*) from userfocus uf where user_id=?";
        return jdbcTemplate.queryForObject(sql, Integer.class,id);
    }


    /**
     * 更新用户信息
     * @param user
     */
    public void update(User user) {
        jdbcTemplate.update(
                "update user set real_name=?,age=?,phone=?,gender=?,info=?,is_secret=?,dept_name=?,dept_id=? where id=?",
                user.getRealName(),
                user.getAge(),
                user.getPhone(),
                user.getGender(),
                user.getInfo(),
                user.getIsSecret(),
                user.getDeptName(),
                user.getDeptId(),
                user.getId());
    }


    /**
     * 更新图片地址
     * @param id
     * @param url
     */
    public void updataPic(Long id, String url) {
        jdbcTemplate.update(
                "update user set pic=? where id=?",
               url, id);
    }


    /**
     * 根据id查询这个用户的关注列表
     * @param id
     * @return
     */
    public List<Long> findFocusIdList(Long id) {
        String sql="select user_focus_id from userfocus where user_id=?";
         List<Long> userfocusIdList= jdbcTemplate.query(sql, new RowMapper<Long>() {
             @Override
             public Long mapRow(ResultSet resultSet, int i) throws SQLException {
                 return resultSet.getLong("user_focus_id");
             }
         }, id);
         return userfocusIdList;
    }


    /**
     * 我的关注列表+分页
     * @param id
     * @param startIndex
     * @param pageSize
     * @return
     */
    public List<User> findFocusPage(Long id, Integer startIndex, Integer pageSize) {
        String sql="select * from user where id in (select user_focus_id from userfocus where user_id=?) limit ?,?";
        return  jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class),id,startIndex,pageSize);

    }


    /**
     * 判断当前用户是否关注过该用户
     * @param id
     * @param focusId
     * @return
     */
    public Integer countByUserIdAndFocusId(Long id, long focusId) {
        String sql="select count(*) from userfocus where user_id=? and user_focus_id=?";
        return  jdbcTemplate.queryForObject(sql, Integer.class,id,focusId);

    }


    /**
     * 取消关注
     * @param id
     * @param focusId
     */
    public void deleteByUserIdAndFocusId(Long id, long focusId) {
        String sql="delete from userfocus where user_id=? and user_focus_id=?";
        jdbcTemplate.update(sql,id,focusId);

    }


    /**
     * 添加关注
     * @param id
     * @param focusId
     */
    public void addByUserIdAndFocusId(Long id, long focusId) {
        String sql="insert into userfocus values (?,?)";
        jdbcTemplate.update(sql,id,focusId);

    }


    /**
     * 根据微信颁发的openid查询用户
     * @param openid
     * @return
     */
    public User findByOpenId(String openid) {
        String sql="select * from user where wx_openid=?";
        try {
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),openid);
        } catch (DataAccessException e) {
            return null;
        }
    }
}
